package com.abe.mvp.view.fragment.base;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.abe.app.AdaptiveFragment;
import com.abe.app.ManagerAPP;
import com.abe.strengthtrainer.R;
import com.abe.utils.NormalUtils;
import com.abe.utils.ToastUtils;


@SuppressWarnings("unchecked")
public abstract class BaseFragment extends AdaptiveFragment {

	private static final int APP_TOAST = 0x001;
	private static final int APP_TOAST_BY_ID = 0x002;
	private static final int GOTO_ACTIVITY = 0x003;
	private static final int GOTO_ACTIVITY_FOR_RESULT = 0x004;

	class BaseHander extends Handler {
		public BaseHander(Looper looper) {
			super(looper);
		}

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case APP_TOAST: {
				ToastUtils.showShort(ManagerAPP.getContext(),
						msg.obj.toString());
			}
				break;
			case APP_TOAST_BY_ID: {
				ToastUtils.showShort(ManagerAPP.getContext(),
						Integer.parseInt(msg.obj.toString()));
			}
				break;
			case GOTO_ACTIVITY: {
				Intent intent = (Intent) msg.obj;
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.fade,
						R.anim.hold);
			}
				break;
			case GOTO_ACTIVITY_FOR_RESULT: {
				Map<String, Object> map = (Map<String, Object>) msg.obj;
				Intent intent = (Intent) map.get("intent");
				int requestCode = (Integer) map.get("requestCode");
				startActivityForResult(intent, requestCode);
				getActivity().overridePendingTransition(R.anim.fade,
						R.anim.hold);
			}
				break;
			}
		}
	}

	private void localSendMessage(Message message) {
		new BaseHander(Looper.getMainLooper()).sendMessage(message);
	}

	public void startActivity(Intent intent) {
		super.startActivity(intent);
		getActivity().overridePendingTransition(R.anim.fade, R.anim.hold);
	}

	private void gotoActivity(Intent intent) {
		localSendMessage(NormalUtils.getMessage(GOTO_ACTIVITY, intent));
	}

	private void gotoActivityForResult(Intent intent, int requestCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("intent", intent);
		map.put("requestCode", requestCode);
		localSendMessage(NormalUtils.getMessage(GOTO_ACTIVITY_FOR_RESULT, map));
	}

	public void showToast(String message) {
		localSendMessage(NormalUtils.getMessage(APP_TOAST, message));
	}

	public void showToast(int message) {
		localSendMessage(NormalUtils.getMessage(APP_TOAST_BY_ID, message));
	}

	public void gotoActivity(Class<?> cls) {
		gotoActivity(cls, null);
	}

	public void gotoActivity(Class<?> cls, Bundle bundle) {
		Intent intent = new Intent(getActivity(), cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		gotoActivity(intent);
	}

	public void gotoActivityForResult(Class<?> cls, int requestCode) {
		gotoActivityForResult(cls, null, requestCode);
	}

	public void gotoActivityForResult(Class<?> cls, Bundle bundle,
			int requestCode) {
		Intent intent = new Intent(getActivity(), cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		gotoActivityForResult(intent, requestCode);
	}
}
